﻿
namespace SiteMapMVC
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Hosting;
    using System.Xml.Linq;
    using Newtonsoft.Json;

    public class SiteMapMvc
    {
        public List<SiteMapObject> SiteMapObject { get; set; }

        public SiteMapMvc(string relativePath = "~/SiteMap.xml")
        {
            var pathToFile = HostingEnvironment.MapPath(relativePath);

            if (string.IsNullOrEmpty(pathToFile)) return;
            var element = XElement.Load(pathToFile);
            var list = element.Elements("siteMapNode");

            SiteMapObject = ReadAllNodes(list);
        }

        private List<SiteMapObject> ReadAllNodes(IEnumerable<XElement> list)
        {
            var xElements = list as IList<XElement> ?? list.ToList();
            if (xElements.Any())
            {
                return xElements.Select(e => new SiteMapObject
                    {
                        Title = e.Attribute("title").Value,
                        Description = e.Attribute("description") != null ? e.Attribute("description").Value : "",
                        Controller = e.Attribute("controller") != null ? e.Attribute("controller").Value : "",
                        Action = e.Attribute("action") != null ? e.Attribute("action").Value : "",
                        RouteValue = e.Attribute("routeValue") != null ? e.Attribute("routeValue").Value : "",
                        Nodes = ReadAllNodes(e.Elements("siteMapNode"))
                    }).ToList();
            }
            return null;
        }

        public static void GetBreadcrumb(string controller, string action, StringBuilder routeValues)
        {
            var sm = new SiteMapMvc();

            Breadcrumblist = new List<SiteMapObject>();
            GetBreadcrumbSiteMapObject(sm.SiteMapObject.ToList(), controller, action, routeValues);
        }

        public static string GetSiteMapPaths(List<SiteMapObject> list)
        {
            var result=new StringBuilder();
            result.Append("<ul>");

            foreach (var n1 in list)
            {

                result.Append("<li>");
                var routeValues = n1.RouteValue.Replace("{", string.Empty)
                                       .Replace("}", string.Empty)
                                       .Replace("'", string.Empty)
                                       .Replace(":","=")
                                       .Replace(",", "&");
                string link = "<a href=\"/" + n1.Controller + "/" + n1.Action + "/" + routeValues + "\">" + n1.Description + "</a>";

                result.Append(link);

                //result.Append(n1.Controller + "->" + n1.Action);
                if (n1.Nodes != null)
                {
                    result.Append(GetSiteMapPaths(n1.Nodes.ToList()));
                }
                    result.Append("</li>");
            }
            result.Append("</ul>");

            return result.ToString();

        }

        public static List<SiteMapObject> Breadcrumblist = new List<SiteMapObject>();

        private static bool GetBreadcrumbSiteMapObject(List<SiteMapObject> listSiteMapObject, string controller,
                                                       string action, StringBuilder routeValues)
        {
            var smoes = listSiteMapObject.FirstOrDefault(p => p.Controller == controller
                                                              && p.Action == action &&
                                                              p.RouteValue == routeValues.ToString());
            if (smoes == null)
            {
                foreach (var smo in listSiteMapObject.Where(smo => smo.Nodes != null &&
                                                                   GetBreadcrumbSiteMapObject(smo.Nodes.ToList(),
                                                                                              controller, action,
                                                                                              routeValues)))
                {
                    if (Breadcrumblist != null) Breadcrumblist.Add(smo);
                    return true;
                }
            }
            else
            {
                if (Breadcrumblist != null) Breadcrumblist.Add(smoes);
                return true;
            }
            return false;
            //return controller + "-" + action;
        }
    }

    public class SiteMapObject
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Controller { get; set; }
        public string Action { get; set; }
        public string RouteValue { get; set; }
        public IEnumerable<SiteMapObject> Nodes { get; set; }
    }

    public class RouteObj
    {
        public int Id { get; set; }
        public int Y { get; set; }
        public string C { get; set; }
        public static object GetObj(string p)
        {
            if (!string.IsNullOrEmpty(p))
            {
                var routeObj = JsonConvert.DeserializeObject<RouteObj>(p);

                if (routeObj.Id > 0 && routeObj.Y == 0 && routeObj.C == null)
                {
                    return new { id = routeObj.Id };
                }
                if (routeObj.Id == 0 && routeObj.Y > 0 && routeObj.C == null)
                {
                    return new { y = routeObj.Y };
                }
                if (routeObj.Id == 0 && routeObj.Y == 0 && routeObj.C != null)
                {
                    return new { c = routeObj.C };
                }
            }
            return new { };
        }
    }

}